package com.zret.oasys.operation.emp;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zret.oasys.dbtools.MD5Util;
import com.zret.oasys.entity.Emp;
import com.zret.oasys.json.entity.MsgJson;

public class CheckPwd implements EmpServletOperation {

	@Override
	public void doOperation(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 设置编码格式
		request.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		// json格式化对象
		Gson gson = new Gson();
		// 传递json信息类
		MsgJson msg = new MsgJson();
		// 输出流
		PrintWriter out = response.getWriter();
		// 获取旧的密码判断
		String oldpwd = "";
		String username = "";
		if(request.getParameter("oldpwd") != null) {
			oldpwd = request.getParameter("oldpwd");
		}
		if(request.getParameter("username") != null) {
			username = request.getParameter("username");
		}
		System.out.println(username);
		System.out.println(oldpwd);
		// MD5的转换
		String MD5oldpwd = MD5Util.getEncodeByMd5(oldpwd);
		System.out.println(MD5oldpwd);
		// 判断用户输入密码是否正确
		List<Emp> list = empService.GetLogin(username, MD5oldpwd);
		if(list.size() > 0) {
			// 验证成功, 旧密码输入正确 
			msg.setCode(200);
			msg.setMsg("密码正确");;
		} else {
			// 密码错误
			msg.setCode(500);
			msg.setMsg("输入的密码错误");;
		}
		// 格式化
		String jsonObj = gson.toJson(msg);
		out.print(jsonObj);
		out.close();
	}

}
